package ru.spb.osll.myfit.client.widget;

import java.util.Vector;

import ru.spb.osll.myfit.client.PageMode;
import ru.spb.osll.myfit.common.Include;
import ru.spb.osll.myfit.common.Page;
import ru.spb.osll.myfit.common.PageElement;
import ru.spb.osll.myfit.common.PageElementType;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Widget;

/**
 * The includeWidget is created based on data from Include. The includeWidget is
 * a DecoratePanel, which contains tableWidgets and other IncludeWidgets.
 * 
 * @author Mariya Gavrilova
 * 
 */
public class IncludeWidget extends PageElementWidget { // Composite implements PageElementWidget {

  private PageMode m_mode;
  /**
   * included page
   */
  private Page includePage;

  /**
   * included page path
   */
  private String includePath;
 
  /**
   * widget, that content all other widgets on the page.
   */
  private PageWidget m_contentWidget;
  
  /**
   * upper panel.
   */
  private DisclosurePanel m_advancedDisclosure;

  private PageElementType m_type;

  /**
   * 
   * @param include
   * @param width
   *          - width of widget
   */
  public IncludeWidget(GWTWidgetFactory gwtWidgetFactory, Include include) {
	  super(gwtWidgetFactory);
    sinkEvents(Event.ONMOUSEUP | Event.ONDBLCLICK | Event.ONCONTEXTMENU);
    createPopupMenu();
    m_type = include.getType();
    includePage = include.getPage();
    includePath = include.getPath();
    initWidget(createContentWidget());
    m_mode = PageMode.EDIT;
  }

  /**
   * create widget, which will be added into a panel (DecoratorPanel)
   * 
   * @return Widget - created widget
   */
  protected Widget createContentWidget() {

    FlexTable layout = m_gwtWidgetFactory.createFlexTable();
    layout.setCellSpacing(6);
    Vector<PageElement> pageElements = includePage.getElements();
    int countPageElement = pageElements.size();
    m_contentWidget = new PageWidget(m_gwtWidgetFactory);
    
    for (int i = 0; i < countPageElement; i++) {      
      m_contentWidget.addPageElementWidget(pageElements.get(i));
    }
    
    m_contentWidget.setMode(PageMode.VIEW);

    String includeTitle = getIncludeName() + " " + getIncludePath(); 
    m_advancedDisclosure = (DisclosurePanel)m_gwtWidgetFactory.createDisclosurePanel(includeTitle, m_contentWidget);
    
    layout.setWidget(0, 0, m_advancedDisclosure);
    DecoratorPanel panel = m_gwtWidgetFactory.createDecoratorPanel();
    panel.setWidget(layout);
    return panel;
  }
  
  protected String getIncludeName(){
    PageElementType type = getType();
    return type.getKey();
  }
  
  protected String getIncludePath(){
    return includePath;
  }
  
  /**
   * Changes the visible state of this DisclosurePanel. 
   * And does it recursively to all sub_includes. 
   * @param isOpen true to "open" the widget, false to close.
   */
  @Override
  public void setOpen (boolean isOpen) {
    m_advancedDisclosure.setOpen(isOpen);
    m_contentWidget.setOpen(isOpen);
  }
  
  public void setMode(PageMode mode) {
    m_mode = mode;
  }
  
  public PageElement save() {
    return new Include(includePath, includePage);
  }
  
  public void setStyle() {
    m_advancedDisclosure.setStyleName("IncludeErrorPanel"); 
  }
  
  protected void onRightClick(Event event) {
    if (!(PageMode.EDIT).equals(m_mode))
      return;
      int x = event.getClientX(); 
      int y = event.getClientY();
      m_rootPopupMenu.setPopupPosition(x, y);
      m_rootPopupMenu.show();
     
  }

  public void onBrowserEvent(Event event) {
    GWT.log("onBrowserEvent", null);
    event.preventDefault();
    switch (DOM.eventGetType(event)) {
    case Event.ONMOUSEUP:
      if (DOM.eventGetButton(event) == Event.BUTTON_LEFT) {
        GWT.log("Event.BUTTON_LEFT", null);
      }

      if (DOM.eventGetButton(event) == Event.BUTTON_RIGHT) {
        GWT.log("Event.BUTTON_RIGHT", null);
        onRightClick(event);
      }
      break;

    case Event.ONDBLCLICK:
      break;

    case Event.ONCONTEXTMENU:
      GWT.log("Event.ONCONTEXTMENU", null);
      break;

    default:
      break;
    }
  }
  
  public PageElementType getType(){
    return m_type;
  }
  
  public PageWidget getPageWidget() {
    return m_contentWidget;
  }

}
